package com.topnav.mc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.topnav.mc.admin.dao.mapper.DeviceLogMapper;
import com.topnav.mc.admin.entity.DeviceLog;
import com.topnav.mc.admin.entity.Organization;
import com.topnav.mc.admin.entity.User;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.DeviceLogBean;
import com.topnav.mc.admin.entity.param.DeviceLogParam;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.admin.service.IDeviceLogService;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.base.mc.McServiceImpl;
import com.topnav.mc.sse.DeviceAlarmQueue;
import com.topnav.mc.sse.EnumSseMessageType;
import com.topnav.mc.sse.MessageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DeviceLogServiceImpl extends McServiceImpl<DeviceLogMapper, DeviceLog, DeviceLogBean, DeviceLogParam> implements IDeviceLogService {
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private DeviceAlarmQueue deviceAlarmQueue;

    @Override
    public void afterSave(DeviceLog entity){
        QueryWrapper<DeviceParam> queryWrapper = new QueryWrapper<DeviceParam>().eq("d.id",entity.getDeviceId());

        DeviceBean bean = deviceService.getOneInfo(queryWrapper,false);

        if (bean != null){
            //给前前端推送
            Map<String,Object> map = new HashMap<>();
            map.put("id",entity.getId());
            map.put("deviceId",entity.getDeviceId());
            map.put("deviceCode",bean.getCode());
            map.put("path",bean.getPath());
            map.put("action",entity.getAction());

            MessageBean messageBean = new MessageBean(EnumSseMessageType.UP_DOWN_LINE_NOTIFY);
            messageBean.setMessage(map);
            messageBean.setUserId(bean.getUserId());
            messageBean.setUserName(bean.getUserName());
            messageBean.setOrgId(bean.getOrgId());
            messageBean.setOrgPath(bean.getPath());
            deviceAlarmQueue.offer(messageBean);
        }
    };
}
